package com.huiquan.section.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.section.service.SectionHospitalService;

@Controller
public class SectionHospitalController {

    @Autowired
    private SectionHospitalService sectionHospitalService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_hospital_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String keyWord = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyWord");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return sectionHospitalService.list(start, codeSearch, nameSearch,countPerPageStr,keyWord);
	}

	@RequestMapping(value = "/section/section_hospital_pre_edit.htm")
	public ModelAndView pre_edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		return sectionHospitalService.edit(id);
	}
	@ResponseBody
	@RequestMapping(value = "/section/section_hospital_import.do")
	public ReturnData sectionImport(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = null;
		try {
			rtn = sectionHospitalService.insertBatch(user, file);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}

	@ResponseBody
	@RequestMapping(value = "/section/submit_hospital_edit.do")
	public ReturnData sectionCodeAdd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String hospitalId = req.getParameter("hospitalId");
		String hospitalName = req.getParameter("hospitalName");
		String hospitalSection = req.getParameter("hospitalSection");
		String sectionId = req.getParameter("sectionId");
		User user = (User) req.getSession().getAttribute("user");
		String changeFlag = req.getParameter("changeFlag");
		ReturnData rtn = null;
		try {
			rtn =sectionHospitalService.sectionHospitalAdd(id,hospitalId,hospitalName,hospitalSection,sectionId,changeFlag,user);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
	
}
